package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IBankFinder;
import com.gears_software.sxdc.dccx.data.finder.IMemberFinder;
import com.gears_software.sxdc.dccx.data.model.BankCardType;
import com.gears_software.sxdc.dccx.data.model.BindBankCard;
import com.gears_software.sxdc.dccx.data.model.MemberModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.Seat;
import com.gears_software.sxdc.dccx.data.model.UserBankCardInformation;
import com.gears_software.sxdc.dccx.data.model.request.RBindBankCard;
import com.gears_software.sxdc.dccx.ui.util.DES3;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import java.util.ArrayList;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by SunMuran on 2017/8/7.
 */
@EActivity(R.layout.activity_add_bank_card)
public class AddBankCardActivity extends Activity {
    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView tv_youjiantou;
    @ViewById
    RelativeLayout rl_bank;
    @ViewById
    EditText et_card_number;
    @ViewById
    EditText et_kaihuhang;
    @ViewById
    TextView tv_bank_name;
    @ViewById
    Button btn_submit;
    @ViewById
    TextView tv_name;
    @RestService
    IBankFinder iBankFinder;
    List<BankCardType> list = new ArrayList<>();
    Context context;
    String imageUrl = "";
    @Extra
    String flag;
    @RestService
    IMemberFinder memberFinder;
    MemberModel memberModel;

    @AfterViews
    void created() {
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        context = this;
        actionbar_title.setText("添加银行卡");
        initIconfont();
        if (flag.equals("bj")) {
            Bundle bundle = this.getIntent().getExtras();
            String name = bundle.getString("name");
            String bank = bundle.getString("bank");
            String cardno = bundle.getString("cardno");
            String account = bundle.getString("account");
            imageUrl = bundle.getString("bankicon");
            tv_name.setText(name);
            tv_bank_name.setText(bank);
            try {
                String card = DES3.decode(cardno);
                et_card_number.setText(card);
            } catch (Exception e) {
                e.printStackTrace();
            }
            et_kaihuhang.setText(account);
        }
        getCurrentMember();
    }

    void initIconfont() {
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        right_icon.setVisibility(View.GONE);
        tv_youjiantou.setTypeface(app.iconfont);
    }

    @Click
    void back_icon() {
        finish();
    }

    @Click
    void rl_bank() {
        getBankCardType();
    }

    @Click
    void btn_submit() {
        if (!et_card_number.getText().toString().matches("\\d{16,19}")) {
            TastyToast.makeText(this, "请输入正确的卡号", app.SHORT, app.ERROR);
        } else if (et_card_number.getText().toString().equals("")) {
            TastyToast.makeText(this, "请输入卡号", app.SHORT, app.ERROR);
        } else if (tv_bank_name.getText().toString().equals("")) {
            TastyToast.makeText(this, "请选择银行", app.SHORT, app.ERROR);
        } else if (et_kaihuhang.getText().toString().equals("")) {
            TastyToast.makeText(this, "请输入开户行", app.SHORT, app.ERROR);
        } else {
            if (flag.equals("bj")) {
                RBindBankCard rBindBankCard = new RBindBankCard();
                rBindBankCard.setBankhead(tv_name.getText().toString());
                rBindBankCard.setBank(tv_bank_name.getText().toString());
                String cardNumber = et_card_number.getText().toString().trim();
                try {
                    String number = DES3.encode(cardNumber);
                    rBindBankCard.setCardno(number);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                rBindBankCard.setBankaccount(et_kaihuhang.getText().toString().trim());
                rBindBankCard.setBankicon(imageUrl);
                updateBankCard(rBindBankCard);

            } else if (flag.equals("add")) {
                try {
                    RBindBankCard rBindBankCard = new RBindBankCard();
                    rBindBankCard.setBank(tv_bank_name.getText().toString());
                    String cardNumber = et_card_number.getText().toString();
                    String number = DES3.encode(cardNumber);
                    rBindBankCard.setCardno(number);
                    rBindBankCard.setBankhead(tv_name.getText().toString());
                    rBindBankCard.setBankaccount(et_kaihuhang.getText().toString());
                    rBindBankCard.setBankicon(imageUrl);
                    addBankCard(rBindBankCard);
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }

    }

    @Background
    public void getBankCardType() {
        ResultModel<List<BankCardType>> res = iBankFinder.getBankCardType();
        showResout(res);
    }

    @UiThread
    void showResout(ResultModel<List<BankCardType>> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                list = res.getData();
                if (list != null && list.size() > 0) {
                    closeKeyboard();
                    OptionsPickerView pickerView = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {
                        @Override
                        public void onOptionsSelect(int options1, int options2, int options3, View v) {
                            //返回的分别是三个级别的选中位置
                            String tx = list.get(options1).getPickerViewText();
                            imageUrl = list.get(options1).getImage();
                            tv_bank_name.setText(tx);
                        }
                    }).setLineSpacingMultiplier(2).build();
                    pickerView.setPicker(list);
                    pickerView.show();
                }

            }
        }
    }

    private void closeKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    @Background
    void addBankCard(RBindBankCard card) {
        ResultModel<BindBankCard> resultModel = iBankFinder.bindBandCard(card);
        showResult(resultModel);
    }

    @UiThread
    void showResult(ResultModel<BindBankCard> resultModel) {
        if (resultModel != null) {
            if (resultModel.getErrno().equals("0")) {
                TastyToast.makeText(this, "绑定银行卡成功", app.SHORT, app.SUCCESS);
                setResult(1);
                finish();
            } else {
                TastyToast.makeText(this, "绑定银行卡失败", app.SHORT, app.ERROR);
            }
        }
    }

    @Background
    void updateBankCard(RBindBankCard card) {
        ResultModel<String> res = iBankFinder.updateBankCard(card);
        show(res);
    }

    @UiThread
    void show(ResultModel<String> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                setResult(1);
                TastyToast.makeText(this, "修改银行卡信息成功", app.SHORT, app.SUCCESS);
                finish();
            } else {
                TastyToast.makeText(this, "修改银行卡信息失败", app.SHORT, app.ERROR);
            }
        }
    }

    @Background
    void getCurrentMember() {
        ResultModel<MemberModel> result = memberFinder.getMember();
        showMemberResult(result);
    }

    @UiThread
    void showMemberResult(ResultModel<MemberModel> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    memberModel = res.getData();
                    if (memberModel.getFname() != null && !memberModel.getFname().equals("")) {
                        tv_name.setText(memberModel.getFname());
                    }
                }
            }
        }
    }
}